# 导入Flask类
from flask import Flask, render_template, request, send_file
import os
import mark
import luqu

from urllib import parse
import re
# Flask函数接收一个参数__name__，它会指向程序所在的包

app = Flask(__name__)
UPLOAD_PATH = os.path.join(os.path.dirname(__file__),'upload')# 定义上传文件路径

ALLOWED_EXTENSIONS = ['xls', 'xlsx']

def allow_file(filename):# 限制上传文件的格式
    return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS

# 装饰器的作用是将路由映射到视图函数 index
@app.route('/', methods=['GET'])
def nav():
    return render_template('nav.html')

# 装饰器的作用是将路由映射到视图函数 index
@app.route('/grade', methods=['POST', 'GET'])
def index():
    if request.method == "POST":
        f = request.files.get('upload')
        if allow_file(f.filename):
            f.save(os.path.join(UPLOAD_PATH, f.filename)) #不可以放计算大小的前面
            try:
                mark.main(os.path.join(UPLOAD_PATH, f.filename))
            except KeyError:
                return render_template('index.html', msg='请检查文件内容，将信息保存为【准考证】和【身份证号】两列')

            try:
                filepath= 'output.xlsx'
                return send_file(filepath, as_attachment=True)
            except PermissionError:
                return render_template('index.html', msg='文件权限错误，无法访问')
            except Exception as e:
                return render_template('index.html', msg=f'发生错误：{str(e)}')

        else:
            print('不符合要求')
            return render_template('index.html', msg="请上传xls或是xlsx文件")

    return render_template('index.html', title='中考成绩查询', address='grade', hint='支持【xls】和【xlsx】类型文件，相关信息请保存为【准考证】和【身份证号】')

@app.route('/result', methods=['POST', 'GET'])
def result():
    if request.method == "POST":
        f = request.files.get('upload')
        if allow_file(f.filename):
            f.save(os.path.join(UPLOAD_PATH, f.filename)) #不可以放计算大小的前面
            try:
                luqu.main(os.path.join(UPLOAD_PATH, f.filename))
            except KeyError:
                return render_template('index.html', msg='请检查文件内容，将信息保存为【考生号】和【身份证号】两列')

            try:
                filepath= 'output.xlsx'
                return send_file(filepath, as_attachment=True)
            except PermissionError:
                return render_template('index.html', msg='文件权限错误，无法访问')
            except Exception as e:
                return render_template('index.html', msg=f'发生错误：{str(e)}')

        else:
            print('不符合要求')
            return render_template('index.html', msg="请上传xls或是xlsx文件")

    return render_template('index.html',title='中考录取结果查询',address='result', hint='支持【xls】和【xlsx】类型文件，相关信息请保存为【考生号】和【身份证号】')

# 装饰器的作用是将路由映射到视图函数 index
@app.route('/VIP-video', methods=['POST', 'GET'])
def index_VIP():
    if request.method == "POST":
        v_link= request.form.get('link')
        ip = parse.quote_plus(v_link.encode('utf-8'))
        get_url = 'https://jx.jsonplayer.com/player/?url=%s' % ip
        return render_template('video.html',link= get_url)
    return render_template('video.html')

# 装饰器的作用是将路由映射到视图函数 index
@app.route('/VIP-video1', methods=['POST', 'GET'])
def index_VIP1():
    if request.method == "POST":
        v_link= request.form.get('link')
        ip = parse.quote_plus(v_link.encode('utf-8'))
        get_url = 'https://www.administratorw.com/video.php?url=%s' % ip
        return render_template('video.html',link= get_url)
    return render_template('video.html')

# 装饰器的作用是将路由映射到视图函数 index
@app.route('/VIP-video2', methods=['POST', 'GET'])
def index_VIP2():
    if request.method == "POST":
        v_link= request.form.get('link')
        ip = parse.quote_plus(v_link.encode('utf-8'))
        get_url = 'https://jx.xmflv.com/?url=%s' % ip
        return render_template('video.html',link= get_url)
    return render_template('video.html')

@app.route('/VIP-video3', methods=['POST', 'GET'])
def index_VIP3():
    if request.method == "POST":
        v_link= request.form.get('link')
        ip = parse.quote_plus(v_link.encode('utf-8'))
        get_url = 'https://yparse.ik9.cc/index.php?url=%s' % ip
        return render_template('video.html',link= get_url)
    return render_template('video.html')

#@app.route('/VIP-video4', methods=['POST', 'GET'])
#def index_VIP4():
#    if request.method == "POST":
#        v_link= request.form.get('link')
#        ip = parse.quote_plus(v_link.encode('utf-8'))
#        get_url = 'https://www.8090g.cn/?url=%s' % ip
#        return render_template('video.html',link= get_url)
#    return render_template('video.html')


#@app.route('/VIP-video5', methods=['POST', 'GET'])
#def index_VIP5():
#    if request.method == "POST":
#        v_link= request.form.get('link')
#        ip = parse.quote_plus(v_link.encode('utf-8'))
#        get_url = 'https://bd.jx.cn/?url=%s' % ip
#        return render_template('video.html',link= get_url)
#    return render_template('video.html')
# Flask应用程序实例的 run 方法 启动 WEB 服务器

if __name__ == '__main__':
    # app.run()  # 可以指定运行的主机IP地址，端口，是否开启调试模式
    app.run(host="0.0.0.0", port=80)#, debug = True
